package com.sumapay.fsb.gensrc.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sumapay.fsb.common.util.CommonUtil;
import com.sumapay.fsb.gensrc.entity.FsbSmsRequest;
import com.sumapay.fsb.gensrc.mapper.FsbSmsRequestMapper;
import com.sumapay.fsb.gensrc.service.IFsbSmsRequestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sumapay.fsb.gensrc.service.IFsbUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author suma
 * @since 2021-02-26
 */
@Service
public class FsbSmsRequestServiceImpl extends ServiceImpl<FsbSmsRequestMapper, FsbSmsRequest> implements IFsbSmsRequestService {

    private IFsbUsersService iFsbUsersService;

    @Value("${rest.http.sms.url}")
    private String smsUrl;


    private RestTemplate restTemplate;

    @Override
    public String sendSms(MultiValueMap<String, Object> requestMap) {
        Map<String,Object> returnMap = new HashMap<>();
        String messageRandomId = requestMap.get("TRADE_ID").get(0).toString();
        returnMap.put("ACTION_RESULT",1);
        String phone_no = requestMap.get("MOBILE").get(0).toString();
        if( requestMap.get("METHOD") !=null &&requestMap.get("METHOD").get(0).toString().equals("Register")){
            if(iFsbUsersService.isExist(phone_no)){
                returnMap.put("ERROR_MESSAGE","该手机号已存在");
                return JSON.toJSONString(returnMap);
            }
        }
        String sms = CommonUtil.randomCode();
        String messageContent = "短信验证码内容："+sms+"(丰收宝验证码)，3分钟有效，请勿泄露！";
        requestMap.add("SMS_CONTENT",messageContent);
        //发送短信验证码
        FsbSmsRequest fsbSmsRequest = new FsbSmsRequest();
        fsbSmsRequest.setMessageRandomCode(sms);
        fsbSmsRequest.setPhoneNo(phone_no);
        fsbSmsRequest.setMessageConetent(messageContent);
        fsbSmsRequest.setStatus(0);
        fsbSmsRequest.setRowCreateTime(LocalDateTime.now());
        fsbSmsRequest.setMessageRandomId(messageRandomId);

        if(baseMapper.insert(fsbSmsRequest)>0){
            ResponseEntity responseEntity = restTemplate.postForEntity(smsUrl,
                    requestMap, String.class, (Object) null);
            System.out.println("-------" + responseEntity.getBody().toString());
            return  responseEntity.getBody().toString();
        }else {
            returnMap.put("ERROR_MESSAGE","服务器异常,请重试!");
            return JSON.toJSONString(returnMap);
        }

    }

    @Override
    public boolean checkSms(MultiValueMap<String, Object> map) {

        QueryWrapper<FsbSmsRequest> queryWrapper = new QueryWrapper();
        queryWrapper.eq("MESSAGE_RANDOM_ID", map.get("SMS_ID").get(0));
        String ss = queryWrapper.getExpression().getSqlSegment();
        System.out.println(ss);
        FsbSmsRequest fsbSmsRequest = baseMapper.selectOne(queryWrapper);
        if(fsbSmsRequest!=null && fsbSmsRequest.getMessageRandomCode().equals(map.get("SMS_CODE").get(0).toString()))
            return true;
        return false;
    }

    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @Autowired
    public void setiFsbUsersService(IFsbUsersService iFsbUsersService){
        this.iFsbUsersService = iFsbUsersService;
    }
}
